package com.ys.web.background.service.impl;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.IdUtil;
import com.ys.base.BaseConstants;
import com.ys.base.EnumType;
import com.ys.utils.base.j2se.DateHandler;
import com.ys.utils.base.j2se.JsonUtil;
import com.ys.utils.base.j2se.Logger;
import com.ys.utils.help.GroupUtils;
import com.ys.utils.redis.Redis;
import com.ys.utils.redis.key.GroupKey;
import com.ys.utils.spring.config.SystemError;
import com.ys.web.background.mapper.BackgroundGroupMapper;
import com.ys.web.background.service.BackgroundGroupService;
import com.ys.web.background.vo.BackgroundGroupListVo;
import com.ys.web.background.vo.GroupParamVo;
import com.ys.web.user.friends.po.BaseGroupInfoPO;
import com.ys.web.ws.bean.MessageResponseBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @ 名称:
 * @ 创建人: dhf
 * @ 参数： null
 * @ 创建时间: 2022/5/10
 * @ 修改人和其它信息
 * @ 返回值:
 */
@Slf4j
@Service
public class BackgroundGroupServiceImpl implements BackgroundGroupService {

    @Autowired
    private BackgroundGroupMapper backgroundGroupMapper;

    @Override
    public List<BackgroundGroupListVo> getGroupList(GroupParamVo groupParamVo) {
        return backgroundGroupMapper.getGroupList(groupParamVo);
    }

    @Override
    public int getGroupListCount(GroupParamVo groupParamVo) {
        return backgroundGroupMapper.getGroupListCount(groupParamVo);
    }

    @Override
    public void updateInfo(String id, int isFreeze) {
        int row = backgroundGroupMapper.updateInfoById(id, isFreeze);
        if (row <= 0) {
            SystemError.wrapBs("500", "操作失败");
        } else {
            String key = GroupKey.groupInfoKey(id);
            BaseGroupInfoPO baseGroupInfoPO = JsonUtil.jsonToBean(Redis.str.get(key), BaseGroupInfoPO.class);
            baseGroupInfoPO.setIsFreeze(isFreeze == 1);
            Redis.str.set(key, JsonUtil.toJson(baseGroupInfoPO));

            ThreadUtil.execAsync(() -> {
                BaseGroupInfoPO cacheGroupInfo = GroupUtils.getCacheGroupInfo(id);
                MessageResponseBean messageResponseBean = new MessageResponseBean();
                messageResponseBean.setGroupName(cacheGroupInfo.getName());
                messageResponseBean.setGroupPic(cacheGroupInfo.getPic());
                messageResponseBean.setMessageId(IdUtil.simpleUUID());
                messageResponseBean.setGroupId(id);
                messageResponseBean.setWindowId(BaseConstants.GROUP_PREFIX + id);
                messageResponseBean.setData(isFreeze == 1 ? "该群已被禁用" : "该群已能正常使用");
                messageResponseBean.setCreateTime(DateHandler.getDate(new Date(), BaseConstants.BASE_FORMAT));
                messageResponseBean.setCode(isFreeze == 1 ? EnumType.MessageCode.SYSTEM_GROUP_DISABLE.getCode() : EnumType.MessageCode.SYSTEM_GROUP_ENABLE.getCode());
                Logger.info("推送内容：" + JsonUtil.toJson(messageResponseBean));
                Set<String> set = Redis.zSet.rangeByScore(GroupKey.groupMembersKey(id),
                        EnumType.GroupIdentity.MASTER.getCode().doubleValue(),
                        EnumType.GroupIdentity.MEMBERS.getCode().doubleValue());
                messageResponseBean.setGroupName(messageResponseBean.getGroupName() + "(" + set.size() + ")");
                set.forEach(x -> GroupUtils.sendIfAbsent(messageResponseBean, x, BaseConstants.GROUP_PREFIX + id));
            });
        }
    }


}
